/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wargame.dao;

import connectionmanager.DB;
import wargame.entity.ProjectInject;

/**
 *
 * @author stanley.2007
 */
public class ProjectInjectDAO {

    public static ProjectInject findById(int id){
        String sql = "SELECT *"
                + " FROM `project_inject` WHERE project_id = " + id;
        DB db = new DB();
        db.connect();
        db.query(sql);
        ProjectInject p = null;
        if (db.next()) {
            p = populate(db);
        }
        db.close();
        return p;
    }

    public static ProjectInject findForTurn(int id, int turn){
        String sql = "SELECT *"
                + " FROM `project_inject` WHERE project_id = " + id + " AND turn=" + turn;
        DB db = new DB();
        db.connect();
        db.query(sql);
        ProjectInject p = null;
        if (db.next()) {
            p = populate(db);
        }
        db.close();
        return p;
    }

    private static ProjectInject populate(DB db){
        ProjectInject p = new ProjectInject();
        p.setProject_id(db.getInt("project_id"));
        p.setDot_net(db.getInt("dot_net"));
        p.setC_sharp(db.getInt("c_sharp"));
        p.setJsp(db.getInt("jsp"));
        p.setPhp(db.getInt("php"));
        p.setRuby(db.getInt("ruby"));
        p.setMysql(db.getInt("mysql"));
        p.setMssql(db.getInt("mssql"));
        p.setOracle(db.getInt("oracle"));
        p.setPrice(db.getInt("price"));
        p.setTurn(db.getInt("turn"));
        return p;
    }

    public static int insert(ProjectInject p){
        String sql = "INSERT INTO project_inject (" +
                "project_id, " +
                "dot_net, " +
                "c_sharp, " +
                "jsp, " +
                "php, " +
                "ruby, " +
                "mysql, " +
                "mssql, " +
                "oracle, " +
                "price, " +
                "turn) " +
                "VALUES ( " +
                p.getProject_id() + "," +
                p.getDot_net() + "," +
                p.getC_sharp() + "," +
                p.getJsp() + "," +
                p.getPhp()  + "," +
                p.getRuby()  + "," +
                p.getMysql()  + "," +
                p.getMssql()  + "," +
                p.getOracle()  + "," +
                p.getPrice()  + "," +
                p.getTurn() + ");";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        int lastInsertedId = db.getLastInsertedID();
        db.close();
        return lastInsertedId;
    }

    public static void update(ProjectInject p){
        String sql = "UPDATE project_inject SET" +
                "dot_net = " + p.getDot_net() + "," +
                "c_sharp = " + p.getC_sharp() + "," +
                "jsp = " + p.getJsp() + "," +
                "php = " + p.getPhp()  + "," +
                "ruby = " + p.getRuby()  + "," +
                "mysql = " + p.getMysql()  + "," +
                "mssql = " + p.getMssql()  + "," +
                "oracle = " + p.getOracle()  + "," +
                "price = " + p.getPrice()  + "," +
                "turn = " + p.getTurn() +
                " WHERE project_id =" + p.getProject_id();
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static void delete(int project_id) {
        String sql = "DELETE FROM project_inject WHERE project_id =" + project_id;
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static void deleteCurrentTurn(int turn) {
        String sql = "DELETE FROM project_inject WHERE turn =" + turn;
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }


}